package com.devunion.bogdan.model.project;

import com.devunion.bogdan.model.event.OntologyPropertyListener;
import com.devunion.bogdan.model.event.PropertyValueEvent;
import com.devunion.bogdan.model.event.PropertyValueListener;

import javax.persistence.*;
import javax.swing.event.EventListenerList;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

/**
 * @author Viktar Liaskovich (devunion@gmail.com)
 */
@Entity
public class OntologyProperty {
    @Id
    @GeneratedValue
    @Column(name = "ontologyPropertyId")
    private long ontologyPropertyId;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "valuesRepr")
    private String valuesRepr;

    protected transient EventListenerList listenerList = new EventListenerList();

    public long getOntologyPropertyId() {
        return ontologyPropertyId;
    }

    public void setOntologyPropertyId(long ontologyPropertyId) {
        this.ontologyPropertyId = ontologyPropertyId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValuesRepr() {
        return valuesRepr;
    }

    public void setValuesRepr(String valuesRepr) {
        this.valuesRepr = valuesRepr;
    }

    public List<String> getValues() {
        return Collections.unmodifiableList(ValueSerializer.deserialize(valuesRepr));
    }

    public void addValue(String value) {
        if (value == null) {
            throw new IllegalArgumentException("OntologyProperty value can't be null");
        }

        final List<String> values = ValueSerializer.deserialize(valuesRepr);
        values.add(value);
        setValuesRepr(ValueSerializer.serialize(values));

        fireOntologyValueAddedEvent(new PropertyValueEvent(this, value));
    }

    public void removeValue(String value) {
        if (value == null) {
            throw new IllegalArgumentException("OntologyProperty value can't be null");
        }

        final List<String> values = ValueSerializer.deserialize(valuesRepr);
        values.remove(value);
        setValuesRepr(ValueSerializer.serialize(values));

        fireOntologyValueRemovedEvent(new PropertyValueEvent(this, value));
    }

    void fireOntologyValueAddedEvent(PropertyValueEvent e) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();

        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 1; i >= 0; i--) {
            if (listeners[i] == OntologyPropertyListener.class) {
                ((PropertyValueListener) listeners[i + 1]).valueAdded(e);
            }
        }
    }

    void fireOntologyValueRemovedEvent(PropertyValueEvent e) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();

        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length - 1; i >= 0; i--) {
            if (listeners[i] == OntologyPropertyListener.class) {
                ((PropertyValueListener) listeners[i + 1]).valueRemoved(e);
            }
        }
    }

    public static class ValueSerializer {
        private static final String SEP = "!@~S_E_P_~@!";

        public static String serialize(List<String> values) {
            return values == null ? null : StringUtils.join(values.iterator(), SEP);
        }

        public static List<String> deserialize(String values) {
            return StringUtils.isEmpty(values) ?
                    new ArrayList<String>() :
                    new ArrayList<String>(Arrays.asList(StringUtils.split(values, SEP)));
        }
    }
}
